﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Threading;
using FuturamaKassa.Objects;

namespace FuturamaKassa {
    class COMWriter {

        private SerialPort port;
        private COMHandler handler;
        private StringComparer stringComparer;
        private Logger logger;

        private bool run;

        private bool check;
        private byte[] byteArray;

        private string message;
        private string command;

        //Readonlies

        private readonly string createInventoryCommand = "6C20S";
        private readonly string getInventoryCommand = "6C21";
        private readonly string getVersionCommand = "1001";

        private readonly int frequency = 500;

        public COMWriter(COMHandler handler, SerialPort port) {
            this.handler = handler;
            this.port = port;

            logger = Logger.Instance;

            stringComparer = StringComparer.Ordinal;

            run = true;

            //startWriting(); 

            inventory();
        }

        /**
         * Function to start writing to the server (This is for the console Application)
         * 
         */
        public void startWriting() {
            Console.WriteLine("Type QUIT to exit");

            while (run) {
                message = Console.ReadLine();
                check = true;

                if (stringComparer.Equals("quit", message)) {
                    run = false;
                }
                else {

                    switch (message) {
                        case "version":
                            command = "1001";
                            break;
                        case "create_inventory":
                            command = "6C20S";
                            break;
                        case "get_inventory":
                            command = "6C21";
                            break;
                        default:
                            Console.WriteLine("Unknown Command");
                            check = false;
                            break;
                    }

                    if (check == false) { continue; }

                    byteArray = RFIDFunctions.buildCommand(command);

                    port.Write(byteArray, 0, byteArray.Length);
                }
            }
        }

        /**
         * A function that runs a loop and get all tags that are scanned by the RFID Reader.
         */
        public void inventory() {

            while (run) {

                createInventory();

                if (Inventory.GetInstance().Size > 0) {

                    getInventory();

                    foreach (string a in Inventory.GetInstance().Products) {
                        Logger.Instance.writeLog("TAG ID: " + a);
                    }
                }
                Thread.Sleep(frequency);
            }
        }

        /**
         * This function write a create inventory command. The RFID Reader will make an inventory of the tags that are currently in range
         */
        private void createInventory() {
            writeCommand(createInventoryCommand);
            Thread.Sleep(750);
        }

        /**
         * Function to get the inventory if there are tags in it.
         */
        private void getInventory() {

            writeCommand(getInventoryCommand);

            Logger.Instance.writeLog("GetInventory Command Send");


            Inventory.GetInstance().Wait = true;
            while (Inventory.GetInstance().Wait) { Thread.Sleep(100); }
        }

        /**
         * Function to get the version number of the RFID Reader
         */
        private void getVersion() {
            writeCommand(getVersionCommand);
        }

        /**
         * A function that builds the right byte array and sends the command to the RFID Reader
         */
        private void writeCommand(string command) {
            byteArray = RFIDFunctions.buildCommand(command);
            port.Write(byteArray, 0, byteArray.Length);
        }
    }
}
